package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.widget.DialogItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by dahai2070 on 2017/3/1 0001.
 */

public class DialogAdapter extends RecyclerView.Adapter<DialogAdapter.MyDialogViewHolder> {
    private Context mContext;
    private List<DialogItem> dialogItems;
    private OnItemClickListener itemClickListener;
    private String lastItemsName = "取消";

    public DialogAdapter(Context mContext, List<DialogItem> dialogItems) {
        super();
        this.mContext = mContext;
        this.dialogItems = dialogItems;
    }

    @Override
    public int getItemCount() {
        return dialogItems.size() + 1;
    }

    @Override
    public MyDialogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.my_business_dialog_item, parent, false);
        return new MyDialogViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyDialogViewHolder holder, final int position) {
        if (position != dialogItems.size()) {

            holder.tvDialogItem.setText(dialogItems.get(position).getDialogItemName());
            holder.ivDialogItemIcon.setImageResource(dialogItems.get(position).getDialogItemIconID());
        } else {
            holder.tvDialogItem.setText(lastItemsName);
            holder.ivDialogItemIcon.setImageResource(R.drawable.cancle);
        }
        if (position == dialogItems.size() - 1) {//倒数第二个，让分割线变粗
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.placeHolder.getLayoutParams();
            params.height = 10;
            holder.placeHolder.setLayoutParams(params);
            holder.placeHolder.setBackgroundColor(mContext.getResources().getColor(R.color.yancy_grey200)  );
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onClick(position);
            }
        });
    }


    class MyDialogViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_dialog_item)
        TextView tvDialogItem;
        @BindView(R.id.place_holder)
        View placeHolder;
        @BindView(R.id.iv_dialog_item_icon)
        ImageView ivDialogItemIcon;

        public MyDialogViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

}
